<?php
namespace app\admin\controller;

use \think\Controller;
use \think\Db;
use \think\Session;
use think\Request;

class Game extends AdminController
{
    // 游戏列表页
    public function index()
    {
        $userName = Session::get('userName');
        $this->assign('userName',$userName);
        $res = Db::table('xt_gamelist g')
                    ->join('xt_gamelist_cate gc','cid = cateid')
                    ->select();
        return $this->assign(['res'=>$res])->fetch('index');
    }

    // 游戏分类列表页
    public function cate()
    {
        $userName = Session::get('userName');
        $this->assign('userName',$userName);
        $res = Db::table('xt_gamelist_cate')->select();
        return $this->assign('res',$res)->fetch('cate');
    }

    // 游戏分类创建页面
    public function createCate()
    {
        return $this->fetch('createCate');
    }

    // 游戏创建页面
    public function create()
    {
        $cate = Db::table('xt_gamelist_cate')->select();
        $userName = Session::get('userName');
        $this->assign('userName',$userName);
        return $this->assign(['cate'=>$cate])->fetch('create');
    }

    //游戏所属分类页面
    public function gameinfo($id)
    {
        $cate = Db::table('xt_gamelist_cate')->where('cid',$id)->find();
        $res= Db::table('xt_gamelist')->where('cateid', $id)->find();
        return $this->assign(['cate'=>$cate, 'res'=>$res])->fetch('gameinfo');
    }

    //游戏编辑页
    public function edit($id)
    {
        $cate = Db::table('xt_gamelist_cate')->select();
        $res= Db::table('xt_gamelist')->where('id', $id)->find();
        return $this->assign(['cate'=>$cate, 'res'=>$res, 'cateid'=>$res['cateid'], 'id'=>$id])->fetch('edit');
    }

    //分类编辑页
    public function editCate($id)
    {
        $cate = Db::table('xt_gamelist_cate')->where('cid',$id)->find();
        return $this->assign(['cate'=>$cate, 'cid'=>$id])->fetch('editCate');
    }


    //执行分类创建
    public function saveCate()
    {
        $request = Request::instance();
        $res = $request->post();
        if(!$res['cateName']){
             $this->error('分类名不能为空'); die;
        }
        $result = Db::table('xt_gamelist_cate')->insert($res);
        if($result > 0){
            $this->success('分类添加成功',url('admin/game/cate')); die;
        }else{
            $this->error('分类添加失败'); die;
        }
    }

    //执行分类更新
    public function updateCate($id)
    {
        $request = Request::instance();
        $res = $request->post();
        if(!$res['cateName']){
            $this->error('分类名不能为空'); die;
        }
        $result = Db::table('xt_gamelist_cate')->where('cid',$id)->update($res);
        if($result > 0){
            $this->success('分类修改成功',url('admin/game/cate')); die;
        }else{
            $this->error('分类修改失败'); die;
        }
    }


    //执行游戏创建
    public function save()
    {
        $request = Request::instance();
        $res = $request->post();
        $res['ctime'] = date('H-m-d H:i:s');

        //图片上传
        $file = $request->file("gImg");
        if ( $file) {
           //移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads/gameimg/');
            if($info){
                // 变化过的文件名称
                $img =  $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $title = $file->getError();
                $this->error($title); die;
            }
            $res['gImg'] = $img;
        }
        
        $result = Db::table('xt_gamelist')->insert($res);
        if($result > 0){
            $this->success('游戏添加成功',url('admin/game/index')); die;
        }else{
            $this->error('游戏添加失败'); die;
        }
    }

    //执行游戏更新
    public function update($id)
    {
        $request = Request::instance();
        $res = $request->post();
        $res['ctime'] = date('Y-m-d H:i:s');
        //图片上传
        $file = $request->file("gImg");

        if ( $file ) {
            // 修改文件图片前 删除旧文件
            $gImg = Db::table('xt_gamelist')->field('gImg')->where('id',$id)->find();
            $pathurl = ROOT_PATH . 'public' . DS . 'uploads/gameimg/'.$gImg['gImg'];
            $dir = substr($gImg['gImg'],0,8);
            $pathdir = ROOT_PATH . 'public' . DS . 'uploads/gameimg/'.$dir;
            if ( is_file( $pathurl ) ) {
                unlink( $pathurl );
                if( count(scandir($pathdir)) == 2){ //目录为空,=2是因为.和..存在
                    rmdir($pathdir); // 删除空目录 
                } 
            }
           //移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads/gameimg/');
            if($info){
                // 变化过的文件名称
                $img =  $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                $title = $file->getError();
                $this->error($title); die;
            }
            $res['gImg'] = $img;
        }
        
        $result = Db::table('xt_gamelist')->where('id',$id)->update($res);
        if($result > 0){
            $this->success('游戏修改成功',url('admin/game/index')); die;
        }else{
            $this->error('游戏修改失败'); die;
        }
    }



    //游戏删除
    public function delete($id)
    {
        $result = Db::table('xt_gamelist')->where('id',$id)->delete();
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的游戏删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的游戏删除失败!';
        }
        return json($info);
    }

    //分类删除
    public function deleteCate($id)
    {
        $res = Db::table('xt_gamelist')->where('cateid',$id)->select();

        if($res){
          $info['status'] = false; 
          $info['id'] = $id;
          $info['info'] = 'ID为:'.$id.'的分类下不为空, 删除失败!';
          return json($info);
            die;
        }

        $result = Db::table('xt_gamelist_cate')->where('cid',$id)->delete();
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的分类删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的分类删除失败!';
        }
        return json($info);
    }


}